<?php
    /*
       错误码列表，以开头编码分类
     
        0 系统错误  1 短信错误   2 用户错误   3 线路错误
        4 订单错误  5 账务错误   6 充值错误   7 提现错误
        8 投诉错误  9 IM错误    10           11
       12 代金券   13           14           15
       16 保险错误 17 群组错误  18           19
       20          21           22 周期线路
    
     */
    function error_code($code){
        $error  =   array(
            '0001'=>'非法请求',
            '0002'=>'参数不完整',
            '0003'=>'系统错误',
            '0004'=>'Token验证失败'
        );
        return $error[$code];
    }
    /*  
     * @param  $gpc 传入参数
     * @return $gpc 转义后的数据
     */
    function saddslashes($gpc){
        $magic_quote = get_magic_quotes_gpc();
        if(empty($magic_quote)){
            if(is_array($gpc)){
                foreach($gpc as $key => $val){
                    $gpc[$key] = saddslashes($val);
                }   
            }else{
                //$gpc = strip_tags(addslashes($gpc));
                $gpc = strip_tags(urldecode($gpc));
                //$gpc = addslashes($gpc);
                //自动过滤Sql的注入语句。
                //$check=preg_match('/select|insert|update|delete|\'|\\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/i',$gpc);
                //$check=preg_match('/select|insert|update|delete|\'|\.\.\/|\.\/|union|into|load_file|outfile/i',$gpc);
                $check=preg_match('/select|insert|update|delete|\'|\.\.\/|\.\/|union|into|table|database|load_file|outfile/i',$gpc);
                if ($check) {
                    DLOG('filter_string='.$gpc,'run','check');
                    return '';
                }
            }   
        }   
        return $gpc;
    } 
    
    function DLOG($log_content='', $log_level='', $log_name='')
    { 

        if(empty($log_level)||empty($log_content))
            return;
        
        if(!in_array($log_level, config('DLOG_LEVEL')))
            return;
        
        
        if(empty($log_name) && $log_level == 'run')
            $log_name = 'run';
    
        $log_dir = config('DLOG_DIR');
        $log_file = $log_dir.$log_name.".".date('Y-m-d').".log";
        
        $request = request();
        //默认每行日志必写的内容，统一在这里处理
        // ZHANGXI 2015/1/7 for regular
        $time = sprintf("%8s.%03d",date('H:i:s'),floor(microtime()*1000));
        //$time = date('H:i:s').".".floor(microtime()*1000);
        $ip = sprintf("%15s",$request->ip());
    
    
        $content_prefix = "[ ".$time." ".$ip." ".$log_level." ".$_SERVER['PATH_INFO']." ] ";
    
        $fp = fopen($log_file, 'a+');
        fwrite($fp, $content_prefix.$log_content." [".getmypid()."]\n");
        fclose($fp);
        return;
    }

    function setMem($key,$value){
            $key = C("MemcacheSign").$key;
            $mem = getMemObj();
            $ret = $mem->set(strval($key), $value, 60); 
            
            if($ret === false) {
                DLOG('mem  return false '.$key.$mem->getResultCode(), 'error', 'mem');
                return false;
            } else {
                return $ret;
            }	
    }
        
    function getMem($key){
            $key = C("MemcacheSign").$key;
            $mem = getMemObj();
            $ret = $mem->get( strval($key) );
            
            if($ret === false) {
                DLOG('mem  return false '.  $mem->getResultCode(), 'error', 'mem');
                return false;
            } else {
                return $ret;
            }
    }
        
    function getMemObj() {
            $memcache = new \Memcached('wechat_mem_pool');
            $ss = $memcache->getServerList ();
            if (empty ( $ss )) {
                $memcache->setOption(\Memcached::OPT_RECV_TIMEOUT, 50);
                $memcache->setOption(\Memcached::OPT_SEND_TIMEOUT, 50);
                $memcache->setOption(\Memcached::OPT_TCP_NODELAY, true);
                $memcache->setOption(\Memcached::OPT_SERVER_FAILURE_LIMIT, 50);
                $memcache->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 50);
                $memcache->setOption(\Memcached::OPT_RETRY_TIMEOUT, 10);
                $memcache->addServer( C("MemcacheIp"), 11211, 1 );
            }
            return $memcache;
    }
    
    /*
        生成百度短链接
        author:JiaXu
        date:2015-07-23
    */
    function ShortUrl($Url){
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/create.php");
        curl_setopt($ch,CURLOPT_POST,true);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        $data=array('url'=>$Url);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        $strRes=curl_exec($ch);
        curl_close($ch);
        $arrResponse=json_decode($strRes,true);
        
        if($arrResponse['status']==0){
            return $arrResponse['tinyurl'];
        }else{
            return false;
        }
    }
    
    /*
        生成32位密码
    */
    function create_password($length = 32)
    {
        // 密码字符集，可任意添加你需要的字符
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    
        $password = '';
        for ( $i = 0; $i < $length; $i++ )
        {
            // 这里提供两种字符获取方式
            // 第一种是使用 substr 截取$chars中的任意一位字符；
            // 第二种是取字符数组 $chars 的任意元素
            // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
            $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
        }
    
        return $password;
    }
	
	/*
		生成Token
		author:JiaXu
		date:2015/08/07
	*/
    function create_token(){
        $Token = create_password(40)."-".create_password(12)."_".create_password(10)."_".create_password(36);
        
        return $Token;
    }

    /*
        生成空数组
        params:
            arr_cnt 空数组数量
        return:
            array[
                [].
                []
            ]
    */
    function createArr($arr_cnt = 1)
    {
        $arr = array();

        for($i = 0; $i < $arr_cnt; $i++){
            $arr[] = array();
        }

        return $arr;
    }